package abc.red.controller;

import abc.red.common.BaseContext;
import abc.red.entity.D;
import abc.red.entity.R;
import abc.red.entity.Warehouse;
import abc.red.service.WarehouseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author YiXia
 */
@Slf4j
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {


    @Autowired
    private WarehouseService service;
    //默认每页数据量
    private final int pageSize = 10;




    /**
     * 获取权限内的仓库的信息
     */
    @PostMapping("/getAllWarehouseByPages")
    public R<IPage> getAllWarehouseByPages(@RequestBody D d) {

        long id = BaseContext.getCurrentId();
        IPage<Warehouse> page = new Page<>(d.getNum1(), pageSize);
        if (id == 1) {
            //超级管理员可以查询所有
            service.page(page);
        } else {
            LambdaQueryWrapper<Warehouse> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Warehouse::getManagerId, id);
            service.page(page, lqw);
        }
        return R.success(page);
    }


    @PostMapping("/add")
    public R<String> add(@RequestBody Warehouse w) {
        boolean end = service.save(w);
        if (end) {
            log.info("打印 WarehouseController-add 方法上传的 Warehouse 对象数据   {}", w.toString());
            return R.success("新增成功");
        } else {
            return R.error("新增失败");
        }
    }

    @PostMapping("/changeStatus")
    public R<String> changeStatus(@RequestBody D d) {
        Warehouse sqlS = service.getById(d.getId());
        sqlS.setState((sqlS.getState() + 1) % 2);
        //防手贱代码，保护超级管理员账号

        boolean end = service.updateById(sqlS);
        log.info("打印 WarehouseController -changeStatus方法上传的 Warehouse 对象数据   {}", sqlS.toString());
        if (end) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }


    @PostMapping("/deleteById")
    public R<String> deleteById(@RequestBody Warehouse w) {

        boolean end = service.removeById(w);
        if (end) {
            log.info("打印 WarehouseController -deleteById方法上传的 Warehouse 对象数据   {}", w.toString());
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }


    @PostMapping("/edit")
    public R<String> editById(@RequestBody Warehouse w) {
        boolean end = service.updateById(w);
        if (end) {
            log.info("打印 WarehouseController -edit方法上传的 Warehouse 对象数据   {}", w.toString());
            return R.success("编辑成功");
        } else {
            return R.error("编辑失败");
        }
    }


    @PostMapping("/getOneById")
    public R<Warehouse> editById(@RequestBody D d) {
        return R.success(service.getById(d.getNum1()));
    }


    @PostMapping("/getAllWarehouseList")
    public List<Warehouse> getAllWarehouseList() {
        long id = BaseContext.getCurrentId();
        LambdaQueryWrapper<Warehouse> lqw = new LambdaQueryWrapper<>();
        if (id == 1) {
            lqw.eq(Warehouse::getState,1);
        } else {
            lqw.eq(Warehouse::getManagerId, id).eq(Warehouse::getState,1);
//            lqw.eq(Warehouse::getManagerId, id);
//            lqw.and((w)->{w.eq(Warehouse::getState,1);});
        }
        log.info("----{}",lqw);
        return service.list(lqw);
    }


}
